package com.hnas.appLogin.service.impl;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hnas.account.dao.AccountDAO;
import com.hnas.account.orm.Account;
import com.hnas.appLogin.dao.VerifyCodeDao;
import com.hnas.appLogin.dto.search.DTOLogin;
import com.hnas.appLogin.service.LoginService;
import com.hnas.appLogin.util.AppTypeConstants;
import com.hnas.appLogin.util.LoginConstants;
import com.hnas.common.util.CommonUtils;
import com.hnas.common.util.PostServerInfoGet;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;
import com.hnas.eCard.dao.AccChannelReDao;
import com.hnas.eCard.dao.ECardChannelInfoDao;
import com.hnas.eCard.orm.AccChannelRe;
import com.hnas.eCard.orm.ECardChannelInfo;

@Component("loginService")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class LoginServiceImpl implements LoginService {

	@Value("#{component['login_effi_time']}")
	private String loginEffiTime;

	@Value("#{component['card_type']}") // 发卡类型
	private String cardType;

	@Value("#{component['merchant_id']}") // 发卡商户id
	private String merchantId;

	@Value("#{component['subCode']}") // 发卡类型
	private String subCode;

	@Value("#{component['acc_card_password']}") // 发卡类型
	private String accCardPassword;

	@Value("#{component['wx_token_valid_time']}")
	private String wxTokenValidTime;

	@Value("#{component['app_token_valid_time']}")
	private String appTokenValidTime;

	@Autowired
	private AccountDAO accountDAO;

	@Autowired
	private VerifyCodeDao verifyCodeDao;

	@Autowired
	private PostServerInfoGet postServerInfoGet;
	@Autowired
	private ECardChannelInfoDao eCardChannelInfoDao;
	@Autowired
	private AccChannelReDao accChannelReDao;

	@Override
	public void loginSuccess1(Account account, DTOLogin dto) throws Exception {
		// 解绑原有的openid
		accountDAO.unBindOpenid(dto.getOpenid());
		String now = DateUtil.dateTimeToString(new Date());
		// 捆绑新的openid 修改登录时间 修改appid
		// accountDAO.updateOpenIdAndTime(account.getId(),
		// dto.getOpenid(),now,dto.getAppid());
		// 逻辑删除验证码
		verifyCodeDao.deleteByPhoneNo(account.getPhoneNo());
	}

	@Override
	public Account register(DTOLogin dto, String appid, String registerType, HttpServletRequest request, String empId)
			throws Exception {
		// 对原来绑定该其他手机绑定的openid进行解绑
		accountDAO.unBindOpenid(dto.getOpenid());
		// 插入用户表
		Account account = new Account();
		account.setAppId(appid);
		account.setLoginType(LoginConstants.VERIFI_LOGIN);
		account.setOpenId(dto.getOpenid());
		account.setPhoneNo(dto.getPhone_no());
		account.setEmpId(empId);
		if (AppTypeConstants.WXXNK.equals(registerType)) {
			account.setAppType(AppTypeConstants.WXXNK);
			account.setWxToken(CommonUtils.getNewToken());
			account.setWxTokenValidEndTime(CommonUtils.nowAddSeconds(Long.parseLong(wxTokenValidTime)));
		}
		account.setCount(0l);
		account.setLastLoginIP(request.getRemoteHost());
		account.setLastLoginTime(DateUtil.dateTimeToString(new Date()));
		accountDAO.saveOrUpdate(account);

		return account;
	}

	@Override
	public Account registerForECard(DTOLogin dto, String appid, String registerType, HttpServletRequest request,
			String empId, String channelId,String nickName,String headPhoto) throws Exception {
		// 对原来绑定该其他手机绑定的openid进行解绑
		accountDAO.unBindOpenid(dto.getOpenid());
		// 插入用户表
		Account account = new Account();
		account.setAppId(appid);
		account.setLoginType(LoginConstants.VERIFI_LOGIN);
		account.setOpenId(dto.getOpenid());
		account.setPhoneNo(dto.getPhone_no());
		account.setEmpId(empId);
		account.setNickName(nickName);
		account.setHeadPhoto(headPhoto);
		if (AppTypeConstants.WXXNK.equals(registerType)) {
			account.setAppType(AppTypeConstants.WXXNK);
			account.setWxToken(CommonUtils.getNewToken());
			account.setWxTokenValidEndTime(CommonUtils.nowAddSeconds(Long.parseLong(wxTokenValidTime)));
		}
		account.setCount(0l);
		account.setLastLoginIP(request.getRemoteHost());
		account.setLastLoginTime(DateUtil.dateTimeToString(new Date()));
		accountDAO.saveOrUpdate(account);

		// 插入账户表(域账号注册时候不发卡)
		if (StringUtil.isEmpty(empId)) {
			// 注册成功之后，如果有渠道id，就绑定渠道id
			if (StringUtil.isNotEmpty(channelId)) {
				ECardChannelInfo channelInfo = this.eCardChannelInfoDao.findByChannelCode(channelId);
				if(channelInfo != null) {
					AccChannelRe entity = new AccChannelRe();
					entity.setAccId(account.getId());
					entity.setChannelId(channelId);
					entity.setId(CommonUtils.getUUID());
					this.accChannelReDao.saveOrUpdate(entity);
				}
			}
		}

		return account;
	}

}
